<?php
namespace app\common\model;

class UserData extends BaseModel{

	public function getUpdateVisitNumAttr($visit_num, $data = array()){
		$user_id   = $user_id ? $user_id : $data['user_id'];
		$visit_num = $visit_num ? $visit_num : $data['visit_num'];

		$this->update_user_data($user_id, 'visit_num');

		return $visit_num + 1;
	}
	/**
	 * 更新用户数据
	 * @param  [type]  $user_id 用户ID
	 * @param  string  $field   更新字段
	 * @param  integer $value   更新值
	 */
	public function update_user_data($user_id, $field = 'question_num', $value = 1){
		if(!$user_id){
			return false;
		}
		if($this->where(array('user_id' => $user_id))->count()){
			if($value > 0){
				$this->where(array('user_id' => $user_id))->setInc($field, abs($value));
			}else{
				$this->where(array('user_id' => $user_id))->setDec($field, abs($value));
			}
		}else{
			$this->insert(array('user_id' => $user_id, $field => $value));
		}
	}
}